package com.hujf.netty.tcp;

import com.hujf.netty.tcp.protocoltcp.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.charset.StandardCharsets;

/**
 * @author Hujf
 * @title: TcpClientHandler
 * @date 2021/2/6 0006下午 2:47
 * @description: TODO
 */
public class TcpClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);
        String s = new String(bytes,StandardCharsets.UTF_8);
        System.out.println("收到服务器回复 "+s);

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //使用客户端发送10条数据
        for(int i=0;i<10;i++){
            ctx.writeAndFlush(Unpooled.copiedBuffer("hello server"+i, CharsetUtil.UTF_8));
        }
    }


}
